﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq.Expressions;
using DevExpress.XtraEditors.DXErrorProvider;
using Loki;
using Loki.UI;
using Loki.UI.Win;
using Luna.UI.Project;

namespace Luna.UI.Win.Project
{
    /// <summary>
    /// Project detail view.
    /// </summary>
    public partial class ProjectDetailView : LunaView, IView<ProjectDetailViewModel>
    {
        public ProjectDetailView()
        {
            InitializeComponent();
            BindingChanged += ProjectDetailView_BindingChanged;

            // Code required
            _Rules.Add(new GridRule<DisplayableTask>()
            {
                Column = COL_Code,
                ErrorMessage = Messages.UIMessages.REQUIRED_TASK_CODE,
                Validator = x => !string.IsNullOrEmpty(x.Code)
            });

            // Name required
            _Rules.Add(new GridRule<DisplayableTask>()
            {
                Column = COL_Name,
                ErrorMessage = Messages.UIMessages.REQUIRED_TASK_NAME,
                Validator = x => !string.IsNullOrEmpty(x.Name)
            });

            // Budget required
            _Rules.Add(new GridRule<DisplayableTask>()
            {
                Column = COL_Allocated,
                ErrorMessage = Messages.UIMessages.REQUIRED_TASK_BUDGET,
                Validator = x => x.Budget.HasValue
            });

            // Name required
            _Rules.Add(new GridRule<DisplayableTask>()
            {
                Column = COL_Start,
                ErrorMessage = Messages.UIMessages.REQUIRED_TASK_START,
                Validator = x => x.StartDate.HasValue
            });

            // Name required
            _Rules.Add(new GridRule<DisplayableTask>()
            {
                Column = COL_End,
                ErrorMessage = Messages.UIMessages.REQUIRED_TASK_END,
                Validator = x => x.EndDate.HasValue
            });
        }

        private List<GridRule<DisplayableTask>> _Rules = new List<GridRule<DisplayableTask>>();

        protected override IEnumerable<DXValidationProvider> Validators
        {
            get
            {
                return new DXValidationProvider[] { VAL_Save };
            }
        }

        void ProjectDetailView_BindingChanged(object sender, EventArgs e)
        {
            COL_Code.Bind<DisplayableTask>(x => x.Code);
            COL_Name.Bind<DisplayableTask>(x => x.Name);
            COL_Allocated.Bind<DisplayableTask>(x => x.Budget, EDT_Amount);
            COL_Start.Bind<DisplayableTask>(x => x.StartDate, EDT_Date);
            COL_End.Bind<DisplayableTask>(x => x.EndDate, EDT_Date);

            TXT_Code.BindValue(ViewModel, x => x.Code);
            TXT_Name.BindValue(ViewModel, x => x.Name);
            TXT_Description.BindValue(ViewModel, x => x.Description);
            TXT_End.BindValue(ViewModel, x => x.EndDate);
            TXT_Start.BindValue(ViewModel, x => x.StartDate);
            TXT_AllocatedAmount.BindValue(ViewModel, x => x.Budget);

            TXT_Allocated.BindValue(ViewModel, x => x.AllocatedPercentage);
            TXT_Remain.BindValue(ViewModel, x => x.Remain);
            TXT_Progress.BindValue(ViewModel, x => x.ProjectAdvance);

            VIEW_Tasks.Configure(ViewModel.Tasks,
                x => string.Format(Messages.UIMessages.DELETE_TASK, x),
                _Rules.ToArray());

            this.WatchChange(ViewModel, x => x.AllocatedPercentage, v => v.ViewModel_AllocatedPercentageChanged);
        }

        private void ViewModel_AllocatedPercentageChanged(object P_Sender, PropertyChangedEventArgs e)
        {
            if (ViewModel != null)
            {
                decimal? L_AllocatedPercentage = ViewModel.AllocatedPercentage;
                if (L_AllocatedPercentage.HasValue && L_AllocatedPercentage.Value > 100)
                {

                    TXT_Allocated.Properties.Appearance.Font = new System.Drawing.Font("Tahoma", 7.8F, System.Drawing.FontStyle.Bold);
                    TXT_Allocated.Properties.Appearance.BackColor = Color.Red;
                    TXT_Allocated.Properties.Appearance.Options.UseFont = true;
                    TXT_Allocated.Properties.Appearance.Options.UseBackColor = true;
                }
                else
                {
                    TXT_Allocated.Properties.Appearance.Options.UseFont = false;
                    TXT_Allocated.Properties.Appearance.Options.UseBackColor = false;
                }
            }
        }

        protected override void BindFormName()
        {
            DataBinder.OneWay(this, f => f.Text, ViewModel, new Expression<Func<ProjectDetailViewModel, object>>[] { v => v.IsDirty, v => v.Name }, new FormattedFormNameConverter(), Messages.UIValues.HEAD_PROJECT);
        }

        public new ProjectDetailViewModel ViewModel
        {
            get { return (ProjectDetailViewModel)base.ViewModel; }
            set { base.ViewModel = value; }
        }
    }
}
